package htsetup

import (
	"core/coreentity"
	"core/coreservice"
	"core/corestruct"
	"core/coreutil"
	"core/myerrors"
)

type menuSetup struct {
	ctx   *corestruct.HttpContext
	lpage *[]coreentity.HtPage
	lmenu *[]coreentity.HtMenu
}

func (p *menuSetup) Setup(ctx1 *corestruct.HttpContext) error {
	p.ctx = ctx1

	var tx = p.ctx.DB.Model(&coreentity.HtPage{})
	var lstsvr = &coreservice.ListService[coreentity.HtPage]{}
	r, err := lstsvr.List(p.ctx, tx)
	if err != nil {
		return err
	}
	p.lpage = r

	tx = p.ctx.DB.Model(&coreentity.HtMenu{})
	var lstsvr2 = &coreservice.ListService[coreentity.HtMenu]{}
	r2, err := lstsvr2.List(p.ctx, tx)
	if err != nil {
		return err
	}
	p.lmenu = r2

	err = p.yfjk()
	if err != nil {
		return err
	}
	err = p.qxxg()
	if err != nil {
		return err
	}
	err = p.yxjk()
	if err != nil {
		return err
	}

	err = p.htcls()
	if err != nil {
		return err
	}
	err = p.htaction()
	if err != nil {
		return err
	}
	err = p.htpage()
	if err != nil {
		return err
	}
	err = p.htcodelog()
	if err != nil {
		return err
	}

	err = p.htmenu()
	if err != nil {
		return err
	}
	err = p.htrole()
	if err != nil {
		return err
	}
	err = p.htuser()
	if err != nil {
		return err
	}

	err = p.hterror()
	if err != nil {
		return err
	}
	err = p.htactionlog()
	if err != nil {
		return err
	}
	err = p.htuserlogin()
	if err != nil {
		return err
	}

	err = p.role()
	if err != nil {
		return err
	}

	return nil
}

func (p *menuSetup) findPage(code string) *coreentity.HtPage {
	for index, _ := range *p.lpage {
		if (*p.lpage)[index].Code == code {
			return &((*p.lpage)[index])
		}
	}
	return nil
}

func (p *menuSetup) findMenu(code string) *coreentity.HtMenu {
	for index, _ := range *p.lmenu {
		if (*p.lmenu)[index].Code == code {
			return &((*p.lmenu)[index])
		}
	}
	return nil
}

func (p *menuSetup) yfjk() error {
	var code = "/yfjk"
	var name = "研发基础"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	m = &coreentity.HtMenu{
		Id:         1100,
		Name:       name,
		Pid:        0,
		HtplatId:   1000,
		Code:       code,
		Lx:         "目录",
		HtpageId:   0,
		HtactionId: 0,
		Imgurl:     "layui-icon-spread-left",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     10,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}

func (p *menuSetup) qxxg() error {
	var code = "/qxxg"
	var name = "权限相关"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	m = &coreentity.HtMenu{
		Id:         1200,
		Name:       name,
		Pid:        0,
		HtplatId:   1000,
		Code:       code,
		Lx:         "目录",
		HtpageId:   0,
		HtactionId: 0,
		Imgurl:     "layui-icon-spread-left",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     20,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}

func (p *menuSetup) yxjk() error {
	var code = "/yxjk"
	var name = "运行监控"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	m = &coreentity.HtMenu{
		Id:         1300,
		Name:       name,
		Pid:        0,
		HtplatId:   1000,
		Code:       code,
		Lx:         "目录",
		HtpageId:   0,
		HtactionId: 0,
		Imgurl:     "layui-icon-spread-left",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     30,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}

func (p *menuSetup) htcls() error {
	var code = "/htcls/index"
	var name = "类型管理"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	var pid = int64(1100)
	var mpage = p.findPage(code)
	if mpage == nil {
		return myerrors.New("page不存在")
	}
	var Sortno = int64(10)

	m = &coreentity.HtMenu{
		Id:         coreutil.IdUtil.GetId(),
		Name:       name,
		Pid:        pid,
		HtplatId:   1000,
		Code:       code,
		Lx:         "菜单",
		HtpageId:   mpage.Id,
		HtactionId: 0,
		Imgurl:     "layui-icon-form",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     Sortno,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}
func (p *menuSetup) htaction() error {
	var code = "/htaction/index"
	var name = "API管理"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	var pid = int64(1100)
	var mpage = p.findPage(code)
	if mpage == nil {
		return myerrors.New("page不存在")
	}
	var Sortno = int64(20)

	m = &coreentity.HtMenu{
		Id:         coreutil.IdUtil.GetId(),
		Name:       name,
		Pid:        pid,
		HtplatId:   1000,
		Code:       code,
		Lx:         "菜单",
		HtpageId:   mpage.Id,
		HtactionId: 0,
		Imgurl:     "layui-icon-form",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     Sortno,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}
func (p *menuSetup) htpage() error {
	var code = "/htpage/index"
	var name = "功能菜单"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	var pid = int64(1100)
	var mpage = p.findPage(code)
	if mpage == nil {
		return myerrors.New("page不存在")
	}
	var Sortno = int64(30)

	m = &coreentity.HtMenu{
		Id:         coreutil.IdUtil.GetId(),
		Name:       name,
		Pid:        pid,
		HtplatId:   1000,
		Code:       code,
		Lx:         "菜单",
		HtpageId:   mpage.Id,
		HtactionId: 0,
		Imgurl:     "layui-icon-form",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     Sortno,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}
func (p *menuSetup) htcodelog() error {
	var code = "/htcodelog/index"
	var name = "研发日志"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	var pid = int64(1100)
	var mpage = p.findPage(code)
	if mpage == nil {
		return myerrors.New("page不存在")
	}
	var Sortno = int64(40)

	m = &coreentity.HtMenu{
		Id:         coreutil.IdUtil.GetId(),
		Name:       name,
		Pid:        pid,
		HtplatId:   1000,
		Code:       code,
		Lx:         "菜单",
		HtpageId:   mpage.Id,
		HtactionId: 0,
		Imgurl:     "layui-icon-form",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     Sortno,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}

func (p *menuSetup) htmenu() error {
	var code = "/htmenu/index"
	var name = "菜单管理"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	var pid = int64(1200)
	var mpage = p.findPage(code)
	if mpage == nil {
		return myerrors.New("page不存在")
	}
	var Sortno = int64(10)

	m = &coreentity.HtMenu{
		Id:         coreutil.IdUtil.GetId(),
		Name:       name,
		Pid:        pid,
		HtplatId:   1000,
		Code:       code,
		Lx:         "菜单",
		HtpageId:   mpage.Id,
		HtactionId: 0,
		Imgurl:     "layui-icon-form",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     Sortno,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}
func (p *menuSetup) htrole() error {
	var code = "/htrole/index"
	var name = "角色管理"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	var pid = int64(1200)
	var mpage = p.findPage(code)
	if mpage == nil {
		return myerrors.New("page不存在")
	}
	var Sortno = int64(20)

	m = &coreentity.HtMenu{
		Id:         coreutil.IdUtil.GetId(),
		Name:       name,
		Pid:        pid,
		HtplatId:   1000,
		Code:       code,
		Lx:         "菜单",
		HtpageId:   mpage.Id,
		HtactionId: 0,
		Imgurl:     "layui-icon-form",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     Sortno,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}
func (p *menuSetup) htuser() error {
	var code = "/htuser/index"
	var name = "用户管理"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	var pid = int64(1200)
	var mpage = p.findPage(code)
	if mpage == nil {
		return myerrors.New("page不存在")
	}
	var Sortno = int64(30)

	m = &coreentity.HtMenu{
		Id:         coreutil.IdUtil.GetId(),
		Name:       name,
		Pid:        pid,
		HtplatId:   1000,
		Code:       code,
		Lx:         "菜单",
		HtpageId:   mpage.Id,
		HtactionId: 0,
		Imgurl:     "layui-icon-form",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     Sortno,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}

func (p *menuSetup) hterror() error {
	var code = "/hterror/index"
	var name = "系统异常"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	var pid = int64(1300)
	var mpage = p.findPage(code)
	if mpage == nil {
		return myerrors.New("page不存在")
	}
	var Sortno = int64(10)

	m = &coreentity.HtMenu{
		Id:         coreutil.IdUtil.GetId(),
		Name:       name,
		Pid:        pid,
		HtplatId:   1000,
		Code:       code,
		Lx:         "菜单",
		HtpageId:   mpage.Id,
		HtactionId: 0,
		Imgurl:     "layui-icon-form",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     Sortno,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}
func (p *menuSetup) htactionlog() error {
	var code = "/htactionlog/index"
	var name = "操作日志"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	var pid = int64(1300)
	var mpage = p.findPage(code)
	if mpage == nil {
		return myerrors.New("page不存在")
	}
	var Sortno = int64(20)

	m = &coreentity.HtMenu{
		Id:         coreutil.IdUtil.GetId(),
		Name:       name,
		Pid:        pid,
		HtplatId:   1000,
		Code:       code,
		Lx:         "菜单",
		HtpageId:   mpage.Id,
		HtactionId: 0,
		Imgurl:     "layui-icon-form",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     Sortno,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}
func (p *menuSetup) htuserlogin() error {
	var code = "/htuserlogin/index"
	var name = "在线用户"
	var m = p.findMenu(code)
	if m != nil {
		return nil
	}
	var pid = int64(1300)
	var mpage = p.findPage(code)
	if mpage == nil {
		return myerrors.New("page不存在")
	}
	var Sortno = int64(30)

	m = &coreentity.HtMenu{
		Id:         coreutil.IdUtil.GetId(),
		Name:       name,
		Pid:        pid,
		HtplatId:   1000,
		Code:       code,
		Lx:         "菜单",
		HtpageId:   mpage.Id,
		HtactionId: 0,
		Imgurl:     "layui-icon-form",
		P1:         "",
		P2:         "",
		P3:         "",
		P4:         "",
		Sortno:     Sortno,
		Memo:       "",
		CrtDt:      corestruct.Now(),
		CrtUid:     1000,
		CrtDid:     1000,
		EdtDt:      corestruct.Now(),
		EdtUid:     1000,
		Gid:        coreutil.IdUtil.UuidStr(),
		Deleted:    0,
	}
	var tx = coreservice.BaseService.Insert(m, p.ctx.DB)
	return tx.Error
}

func (p *menuSetup) role() error {
	var db = p.ctx.DB
	tx := db.Model(coreentity.HtMenu{}).
		Joins("left join Htrolemenu rm on  HtMenu.id=rm.htmenu_id and rm.htrole_id=1000").
		Select("HtMenu.id").
		Where("rm.id is null")
	var fl1 = &coreservice.ListService[coreentity.HtMenu]{}
	lmenu, err := fl1.List(p.ctx, tx)
	if err != nil {
		return err
	}
	for _, v := range *lmenu {
		var m = &coreentity.HtRoleMenu{
			Id:       v.Id,
			HtroleId: 1000,
			HtmenuId: v.Id,
		}
		tx := coreservice.BaseService.Insert(m, db)
		if tx.Error != nil {
			return tx.Error
		}
	}

	tx = db.Model(coreentity.HtMenu{}).
		Joins(" inner join htpage pg on  pg.id=HtMenu.htpage_id and pg.deleted=0" +
			" inner join htpageauth auth on pg.id=auth.htpage_id and auth.deleted=0 " +
			" left join Htrolemenu rm on   auth.id=rm.htmenu_id  and rm.htrole_id =1000 ").
		Select("auth.id").
		Where("rm.id is null")
	var fl2 = &coreservice.ListService[coreentity.HtMenu]{}
	lmenu2, err := fl2.List(p.ctx, tx)
	if err != nil {
		return err
	}
	for _, v := range *lmenu2 {
		var m = &coreentity.HtRoleMenu{
			Id:       v.Id,
			HtroleId: 1000,
			HtmenuId: v.Id,
		}
		tx := coreservice.BaseService.Insert(m, db)
		if tx.Error != nil {
			return tx.Error
		}
	}

	return nil
}
